package io.renren.common.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import io.renren.common.constans.TaoBaoUrlConstans;
import io.renren.modules.app.entity.GoodCouponEntity;
import io.renren.modules.sys.entity.SysCodeEntity;
import io.renren.modules.sys.service.SysCodeService;

public class GoodCouponUtil {
	
	public static PropertyUtil propertyUtil = PropertyUtil.getInstance();
	
	public static JSONArray get(String goodName,Integer page_no,Integer page_size,String cat){
		SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Map<String, String> params= new HashMap<String, String>();
		params.put("method", TaoBaoUrlConstans.NAME_TAOBAO_ITEM_COUPON_GET);
		params.put("sign_method", propertyUtil.getValue("signMethod"));
		params.put("app_key", propertyUtil.getValue("appKey"));
		String date=f.format(new Date());
		params.put("timestamp", date);
		params.put("v", propertyUtil.getValue("v"));
		params.put("format", propertyUtil.getValue("format"));
		params.put("adzone_id", propertyUtil.getValue("adzoneId"));
		params.put("page_size", page_size+"");
		params.put("page_no", page_no+"");
		if(!StringUtil.isEmpty(goodName)){
			params.put("q", goodName);
		}
		if(!StringUtil.isEmpty(cat)){
			params.put("cat", cat);
		}
		JSONObject result = JSONObject.parseObject(TaobaoAPISignUtil.getResult(params));
		JSONObject result1 = result.getJSONObject("tbk_dg_item_coupon_get_response");
		JSONObject result2  = result1.getJSONObject("results");
		System.out.println("淘宝商品————————————————————————"+result2);
		return  result2.getJSONArray("tbk_coupon");
	}
	
	@SuppressWarnings("unchecked")
	public static List<GoodCouponEntity> getGoodCoupon(String goodName,Integer page_no,Integer page_size,String cat,SysCodeService sysCodeService) throws ParseException{
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-mm-dd");
		List<GoodCouponEntity> goodCoupons = new ArrayList<GoodCouponEntity>();
		JSONArray array = get(goodName,page_no,page_size,cat);
		for(int i=0; i<array.size(); i++){
			Map<String, String> coupon = (Map<String, String>)array.get(i);
			GoodCouponEntity goodCoupon = new GoodCouponEntity();
			Long category = MapUtils.getLong(coupon, "category");
			goodCoupon.setCategory(category);
			SysCodeEntity sysCodeEntity = sysCodeService.getByCode(category);
			goodCoupon.setTypeName(sysCodeEntity == null ? category+"" : sysCodeEntity.getName());
			goodCoupon.setCommissionRate(MapUtils.getString(coupon, "commission_rate"));
			goodCoupon.setCouponClickUrl(MapUtils.getString(coupon, "coupon_click_url"));
			goodCoupon.setCouponEndTime(dateFormat.parse(coupon.get("coupon_end_time")));
			String couponInfo = MapUtils.getString(coupon, "coupon_info");
			goodCoupon.setCouponInfo(couponInfo);
			if(!StringUtil.isEmpty(couponInfo) ){
				goodCoupon.setCouponPrice(couponInfo.substring(couponInfo.indexOf("减")+1, couponInfo.indexOf("元", couponInfo.indexOf("减"))));
			}
			goodCoupon.setCouponRemainCount(MapUtils.getLong(coupon, "coupon_remain_count"));
			goodCoupon.setCouponStartTime(dateFormat.parse(coupon.get("coupon_start_time")));
			goodCoupon.setCouponTotalCount(MapUtils.getLong(coupon, "coupon_total_count"));
			goodCoupon.setItemDescription(MapUtils.getString(coupon, "item_description"));
			goodCoupon.setItemUrl(MapUtils.getString(coupon, "item_url"));
			goodCoupon.setNick(MapUtils.getString(coupon, "nick"));
			goodCoupon.setNumIid(MapUtils.getLong(coupon, "num_iid"));
			goodCoupon.setPictUrl(MapUtils.getString(coupon, "pict_url"));
			goodCoupon.setSellerId(MapUtils.getInteger(coupon, "seller_id"));
			goodCoupon.setShopTitle(MapUtils.getString(coupon, "shop_title"));
			goodCoupon.setZkFinalPrice(MapUtils.getString(coupon, "zk_final_price"));
			goodCoupon.setVolume(MapUtils.getLong(coupon, "volume"));
			goodCoupon.setUserType(MapUtils.getInteger(coupon, "user_type"));
			goodCoupon.setTitle(MapUtils.getString(coupon, "title"));
			goodCoupon.setSmallImagesth(MapUtils.getString(coupon, "small_imagesth"));
			goodCoupon.setSmallImagest(MapUtils.getString(coupon, "small_imagest"));
			goodCoupon.setSmallImageso(MapUtils.getString(coupon, "small_imageso"));
			goodCoupon.setSmallImagesf(MapUtils.getString(coupon, "small_imagesf"));
			goodCoupons.add(goodCoupon);
		}
		return goodCoupons;
	}
	
	
	

}
